﻿using Handy_WPF.MVVM;
using HandyControl.Data;
using Kxx_Log.Log;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Handy_DNS.ViewModel
{
    internal class LogViewModel : PageBaseViewModel<LogModel>
    {
        public LogViewModel()
        {
            DataBind();

            new Task(DataRefresh).Start();
        }

        #region 数据绑定
        /// <summary>
        /// 数据绑定
        /// </summary>
        private async void DataBind()
        {
            base.TotalCount = DNSLog.LogList.Count;
            base.DataList = DNSLog.LogList.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
        }
        #endregion

        #region 命令
        private BaseCommand pageUpdatedCommand;
        /// <summary>
        /// 页码改变命令
        /// </summary>
        public BaseCommand PageUpdatedCommand
        {
            get
            {
                if (pageUpdatedCommand == null)
                {
                    pageUpdatedCommand = new BaseCommand(new Action<object>(o =>
                    {
                        int index = ((FunctionEventArgs<int>)o).Info;
                        base.PageIndex = index;
                        DataList = DNSLog.LogList.Skip((index - 1) * PageSize).Take(PageSize).ToList();
                    }));
                }
                return pageUpdatedCommand;
            }
        }
        #endregion

        #region 数据刷新
        private async void DataRefresh()
        {
            while (true)
            {
                if (base.TotalCount != DNSLog.LogList.Count)
                {
                    base.TotalCount = DNSLog.LogList.Count;
                    base.DataList = DNSLog.LogList.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
                }

                await Task.Delay(1000);
            }
        }
        #endregion
    }
}
